package com.gable.exhibition.service.impl;

import com.gable.common.util.CalculationUtil;
import com.gable.common.util.ReturnResultUtil;
import com.gable.exhibition.dao.LargeScreenDao;
import com.gable.exhibition.service.LargeScreenService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.LargeScreenView;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lesson on 2018/2/27.
 */
@Service("largeScreenService")
public class LargeScreenServiceImpl implements LargeScreenService {

    @Autowired
    LargeScreenDao largeScreenDao;

    @Value("${provinceId}")
    Long provinceId;

    Logger logger = Logger.getLogger(this.getClass());

    @Override
    public ReturnResultUtil qeuryLargeScreenData() {
        LargeScreenView largeScreenView = new LargeScreenView();
        largeScreenView.setDeviceCountAndPriceMap(deviceCountAndPrice()); //获取设备数量和设备金额
        largeScreenView.setInsideOutsideDeviceCountList(insideOutsideDeviceCount()); //获取保内设备和保外设备有多少台
        Map<String,Integer>  repairAndUpkeepDeviceCountMap = repairAndUpkeepDeviceCount();
        largeScreenView.setRepairAndUpkeepDeviceCountMap(repairAndUpkeepDeviceCountMap);//获取故障次数(维修记录总和)
        largeScreenView.setRepairRatioMap(repairRatioMap(repairAndUpkeepDeviceCountMap));//获取维修率
        Map<String,Integer> upkeepDegreeDetailMap = queryUpkeepDegreeDetail(); //获取总的保养次数，当前月的保养次数，同比月的保养次数， 当前年的保养次数，同比年的保养次数
        largeScreenView.setUpkeepDegreeDetailMap(upkeepDegreeDetailMap);
        largeScreenView.setUpkeepRatioMap(upkeepRatio(upkeepDegreeDetailMap));//获取当前年，当前月的保养同比率
        largeScreenView.setRepairUpkeepInstallByYearMap( repairUpkeepInstallByYearOrMonth(1));// 获取今年的维修，保养，安装，总数，已完成,完成比例
        largeScreenView.setRepairUpkeepInstallByMonthMap( repairUpkeepInstallByYearOrMonth(2));//获取当月的维修，保养，安装，总数，已完成,完成比例
        largeScreenView.setCityContractCompleteRatioList(queryCityContractCompleteRatio()); //获取城市下厂商合同完成率by城市  合同数量，已完成数量 完成比例
        largeScreenView.setRepartCountByDeviceStatusList(repartCountByDeviceStatus()); //获取紧急维修的设备, 紧急  device_status = 1502 高危  device_status = 1501
        largeScreenView.setNoInstallAndUpkeepCompleteCountMap(queryNoInstallAndUpkeepCompleteCount()); //获取未安装完成和未保养完成的数据
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,largeScreenView);
    }

    @Override
    public ReturnResultUtil queryRepairByDeviceStatus(Long deviceStatus) {
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,largeScreenDao.queryRepairByDeviceStatus(deviceStatus));
    }

    //获取设备数量和设备金额
    private Map<String,Object>  deviceCountAndPrice(){
        Map<String,Object>  deviceCountAndPriceMap =  largeScreenDao.queryDeviceCountAndPrice();
        double value =  CalculationUtil.divide(deviceCountAndPriceMap.get("totlePrice"),10000,2,RoundingMode.HALF_UP);
        deviceCountAndPriceMap.put("totlePrice",value);
        logger.info("--------设备数量:"+deviceCountAndPriceMap.get("totleCount")+",设备金额:"+deviceCountAndPriceMap.get("totlePrice")+",设备金额转万元："+deviceCountAndPriceMap.get("totlePrice"));
        return  deviceCountAndPriceMap;
    }



    //获取保内设备和保外设备有多少台
    private  List<Map<String,Integer>> insideOutsideDeviceCount(){
        List<Map<String,Integer>> insideOutsideDeviceCountList = largeScreenDao.InsideOutsideDeviceCount();
        for(Map<String,Integer> map : insideOutsideDeviceCountList){
            if(map.get("type") == 1){
                logger.info("--------保内设备有："+ map.get("counts")+"台");
            }else{
                logger.info("--------保外设备有："+ map.get("counts")+"台");
            }
        }
        return insideOutsideDeviceCountList;
    }

    //获取故障次数(维修记录总和)
    private Map<String,Integer> repairAndUpkeepDeviceCount(){
        Map<String,Integer> repairAndUpkeepDeviceCountMap = largeScreenDao.queryRepairAndDeviceCount();
        logger.info("--------维修数量:"+repairAndUpkeepDeviceCountMap.get("repairCount")+",设备数量:"+repairAndUpkeepDeviceCountMap.get("deviceCount"));
        return repairAndUpkeepDeviceCountMap;
    }

    //获取维修的故障率
    private  Map<String,Double> repairRatioMap(Map<String,Integer>  repairAndUpkeepDeviceCountMap){
        double reapirRatio = CalculationUtil.divide(repairAndUpkeepDeviceCountMap.get("repairCount"),repairAndUpkeepDeviceCountMap.get("deviceCount"),5, RoundingMode.HALF_UP);
        reapirRatio = CalculationUtil.multiply(reapirRatio,1000);
        logger.info("------维修率：" + reapirRatio);
        Map<String,Double> ratioMap = new HashMap<>();
        ratioMap.put("reapirRatio",reapirRatio);
        return ratioMap;
    }

    //获取总的保养次数，当前月的保养次数，同比月的保养次数， 当前年的保养次数，同比年的保养次数
    private Map<String,Integer> queryUpkeepDegreeDetail(){
        Map<String,Integer> upkeepDegreeDetailMap = largeScreenDao.queryBasisUpkeep();
        logger.info("-----------保养次数明细");
        logger.info("-------保养总数:" + upkeepDegreeDetailMap.get("upkeepTotle"));
        logger.info("-------保养当月数:" + upkeepDegreeDetailMap.get("sameMonth"));
        logger.info("-------保养同比月数:" + upkeepDegreeDetailMap.get("basisMonth"));
        logger.info("-------保养当前年数:" + upkeepDegreeDetailMap.get("sameYear"));
        logger.info("-------保养同比年数:" + upkeepDegreeDetailMap.get("sameYear"));
        return upkeepDegreeDetailMap;
    }

    //获取当前年，当前月的保养同比率           同比算法(当前月-同比月)/同比月
    private Map<String,Double> upkeepRatio(Map<String,Integer>  upkeepDegreeDetailMap){
        Map<String,Double> upkeepRatioMap = new HashMap<>();
        if(upkeepDegreeDetailMap.get("basisMonth") == 0){
            upkeepRatioMap.put("upkeepMonthRatio",(double)100);
        }else{
            double monthvalue1 = CalculationUtil.subtract(upkeepDegreeDetailMap.get("sameMonth"),upkeepDegreeDetailMap.get("basisMonth"));
            double monthvalue2 = CalculationUtil.divide(monthvalue1,upkeepDegreeDetailMap.get("basisMonth"),4,RoundingMode.HALF_UP);
            //做% 运算
            double upkeepMonthRatio = CalculationUtil.multiply(monthvalue2,100);
            upkeepRatioMap.put("upkeepMonthRatio",upkeepMonthRatio);
        }

        if(upkeepDegreeDetailMap.get("sameYear") == 0){
            upkeepRatioMap.put("upkeepYearRatio",(double)100);
        }else{
            double yearvalue1 = CalculationUtil.subtract(upkeepDegreeDetailMap.get("sameYear"),upkeepDegreeDetailMap.get("sameYear"));
            double yearvalue2 = CalculationUtil.divide(yearvalue1,upkeepDegreeDetailMap.get("basisYear"),4,RoundingMode.HALF_UP);
            //做% 运算
            double upkeepYearRatio = CalculationUtil.multiply(yearvalue2,100);
            upkeepRatioMap.put("upkeepYearRatio",upkeepYearRatio);
        }
        logger.info("------当年同比率:" + upkeepRatioMap.get("upkeepYearRatio"));
        logger.info("------当月同比率:" + upkeepRatioMap.get("upkeepMonthRatio"));
        return upkeepRatioMap;
    }


    //获取今年或者当月的维修，保养，安装，总数，未完成，比例
    //type = 1 是当年  type = 2 是当月
    private Map<String,Object> repairUpkeepInstallByYearOrMonth(int type){
        Map<String,Object> map = null;
        if(type == 1){
           map = largeScreenDao.repairUpkeepInstallByYear();
        }else if(type == 2){
            map = largeScreenDao.repairUpkeepInstallByMonth();
        }else{
            return null;
        }
        int repairTotel = Integer.parseInt(map.get("repairTotel").toString());
        int completeRepairTotel = Integer.parseInt(map.get("completeRepairTotel").toString());
        int installTotel = Integer.parseInt(map.get("installTotel").toString());
        int completeInstallTotel = Integer.parseInt(map.get("completeInstallTotel").toString());
        int upkeepTotel = Integer.parseInt(map.get("upkeepTotel").toString());
        int completeUpkeepTotel = Integer.parseInt(map.get("completeUpkeepTotel").toString());

        if(repairTotel == 0){
            map.put("completeRepairRatio",0);
        }else{
            double value1 = CalculationUtil.divide(completeRepairTotel,repairTotel,4,RoundingMode.HALF_UP);
            double value2 = CalculationUtil.multiply(value1,100);
            map.put("completeRepairRatio",value2);
        }

        if(installTotel == 0){
            map.put("completeInstallRatio",0);
        }else{
            double value1 = CalculationUtil.divide(completeInstallTotel,installTotel,4,RoundingMode.HALF_UP);
            double value2 = CalculationUtil.multiply(value1,100);
            map.put("completeInstallRatio",value2);
        }

        if(upkeepTotel == 0){
            map.put("completeUpkeepRatio",0);
        }else{
            double value1 = CalculationUtil.divide(completeUpkeepTotel,upkeepTotel,4,RoundingMode.HALF_UP);
            double value2 = CalculationUtil.multiply(value1,100);
            map.put("completeUpkeepRatio",value2);
        }

        if(type == 1){
            logger.info("-----今年的维修次数:"+repairTotel + ",今年的维修完成次数:"+ completeRepairTotel + ",今年的安装次数："+installTotel+",今年的安装完成次数:"+completeInstallTotel
            +"今年的保养次数:"+ upkeepTotel + ",今年已经完成的保养次数："+ completeUpkeepTotel);
            logger.info("------今年维修完成率:" + map.get("completeRepairRatio"));
            logger.info("------今年安装完成率:" + map.get("completeInstallRatio"));
            logger.info("------今年保养完成率:" + map.get("completeUpkeepRatio"));
        }else {
            logger.info("-----当月的维修次数:"+repairTotel + ",当月的维修完成次数:"+ completeRepairTotel + ",当月的安装次数："+installTotel+",当月的安装完成次数:"+completeInstallTotel
                    +"当月的保养次数:"+ upkeepTotel + ",当月已经完成的保养次数："+ completeUpkeepTotel);
            logger.info("------当月维修完成率:" + map.get("completeRepairRatio"));
            logger.info("------当月安装完成率:" + map.get("completeInstallRatio"));
            logger.info("------当月保养完成率:" + map.get("completeUpkeepRatio"));
        }
        return map;
    }

    //获取城市下厂商合同完成率by城市  合同数量，已完成数量 完成比例
    private  List<Map<String,Object>> queryCityContractCompleteRatio(){
        List<Map<String,Object>> list = largeScreenDao.queryCityContractCompleteRatio(provinceId == null ? 330000L : provinceId);
        for(Map<String,Object> map : list){
                double value = CalculationUtil.multiply(map.get("contractCompleteRatio"),100);
                map.put("contractCompleteRatio",value);
                logger.info("-----合同总数："+map.get("totleQuantity") + ",完成数量:"+ map.get("completeQuantity")+ ",完成率:"+ map.get("contractCompleteRatio"));
        }
        return  list;
    }

    //获取紧急维修的设备, 紧急  device_status = 1502 高危  device_status = 1501
    private List<Map<String,Integer>> repartCountByDeviceStatus(){
        List<Map<String,Integer>> list = largeScreenDao.queryRepartCountByDeviceStatus();
        for(Map<String,Integer> map : list){
            if(map.get("type") == 1){
                logger.info("-----紧急维修的有："+ map.get("counts")+"台");
            }else{
                logger.info("-----高危维修的有："+ map.get("counts")+"台");
            }
        }
        return list;
    }

    //获取未安装完成和未保养完成的数据
    private Map<String,Integer> queryNoInstallAndUpkeepCompleteCount(){
        Map<String,Integer> map = largeScreenDao.queryNoInstallCompleteAndNoUpkeepComplete();
        logger.info("----未完成的安装数量："+ map.get("noInstallComplete"));
        logger.info("----未完成的保养数量:" + map.get("noUpkeepComplete"));
        return map;
    }
}
